package org.cresource.quartz.job;

import org.cresource.order.domain.AdoptOrder;
import org.cresource.order.service.IAdoptOrderService;
import org.cresource.pay.constants.PayConstants;
import org.cresource.pay.domain.PayBill;
import org.cresource.pay.service.IPayBillService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class MainJob implements Job {

    @Autowired
    private IPayBillService payBillService;
    @Autowired
    private IAdoptOrderService adoptOrderService;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //获取定时器里面的数据
        JobDataMap jobDataMap = context.getMergedJobDataMap();

        Map<String,Object> params = (Map<String, Object>) jobDataMap.get("params");
        //获取定时任务的订单号
        String orderSn = params.get("orderSn").toString();

        //执行 修改订单状态的业务逻辑
        //1.改订单的状态
        System.out.println("ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
        PayBill payBill = payBillService.loadByUnionPaySn(orderSn);
        payBill.setState(-1);
        payBill.setUpdateTime(new Date());

        payBillService.update(payBill);
        if(PayConstants.BUSINESS_TYPE_ADOPT.equals(payBill.getBusinessType())){//领养订单
            Long orderId = payBill.getBusinessKey();
            AdoptOrder order = adoptOrderService.getById(orderId);
            order.setState(-1);

            adoptOrderService.update(order);
        }

    }
}
